#region Using

using System.IO;
using System.Net;
using System.Text;

#endregion

namespace WSMon.Core.Helpers
{
    public class HttpRequestHelper
    {
        public static HttpWebResponse GetWebResponse(string url, NetworkCredential networkCredential)
        {
            var webRequest = (HttpWebRequest) WebRequest.Create(url);
            webRequest.Timeout = 25000;
            webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.1; .NET CLR 1.1.4322)";

            if (networkCredential != null)
                webRequest.Credentials = networkCredential;

            return (HttpWebResponse) webRequest.GetResponse();
        }

        public static string GetSourceCodeFromWebResponse(WebResponse webResponse, Encoding encoding)
        {
            using (var streamReader = new StreamReader(webResponse.GetResponseStream(), encoding))
                return streamReader.ReadToEnd();
        }

        public static string GetSourceCode(string url, NetworkCredential networkCredential, Encoding encoding)
        {
            return GetSourceCodeFromWebResponse(GetWebResponse(url, networkCredential), encoding);
        }
    }
}